package com.bzkj.basicinfo.service.allocation.impl;

import com.bzkj.basicinfo.domain.allocation.WmsStockAllocationEntity;
import com.bzkj.basicinfo.mapper.allocation.WmsStockAllocationDetailMapper;
import com.bzkj.basicinfo.mapper.allocation.WmsStockAllocationMapper;
import com.bzkj.basicinfo.service.allocation.WmsStockAllocationService;
import com.bzkj.common.utils.PageUtils;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.utils.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * WMS库存分配Service实现
 *
 * @author system
 * @date 2025-10-22
 */
@Service
public class WmsStockAllocationServiceImpl implements WmsStockAllocationService {

    @Autowired
    private WmsStockAllocationMapper allocationMapper;

    @Autowired
    private WmsStockAllocationDetailMapper allocationDetailMapper;

    @Override
    public TableDataInfo getAllocationList(WmsStockAllocationEntity entity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WmsStockAllocationEntity> list = allocationMapper.selectAllocationList(entity);
        PageInfo<WmsStockAllocationEntity> pageInfo = new PageInfo<>(list);
        return PageUtils.getTableDataInfo(pageInfo);
    }

    @Override
    public WmsStockAllocationEntity getAllocationDetail(Long id) {
        return allocationMapper.selectAllocationById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int executeAllocation(Long outboundOrderId, String strategy) {
        // 生成分配单号：ALLOC + YYYYMMDDHHMMSS
        String allocationNo = "ALLOC" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        // 创建分配主表
        WmsStockAllocationEntity entity = new WmsStockAllocationEntity();
        entity.setAllocationNo(allocationNo);
        entity.setOutboundOrderId(outboundOrderId);
        entity.setAllocationStrategy(strategy != null ? strategy : "FIFO");
        entity.setAllocationStatus("pending");
        entity.setAllocator(SecurityUtils.getUsername());
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(new Date());

        int result = allocationMapper.insertAllocation(entity);

        // TODO: 实现分配逻辑
        // 1. 查询出库单明细
        // 2. 根据策略查询可用库存
        // 3. 创建分配明细
        // 4. 创建库存锁定记录
        // 5. 更新分配状态为completed

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelAllocation(Long id, String reason) {
        // TODO: 释放库存锁定

        WmsStockAllocationEntity entity = new WmsStockAllocationEntity();
        entity.setId(id);
        entity.setAllocationStatus("cancelled");
        entity.setCancelReason(reason);
        entity.setCancelTime(new Date());
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(new Date());
        return allocationMapper.updateAllocation(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAllocation(Long id) {
        // 删除明细
        allocationDetailMapper.deleteDetailsByAllocationId(id);
        // 删除主表
        return allocationMapper.deleteAllocationById(id);
    }
}
